<template>
  <div class="settings">
    <van-nav-bar safe-area-inset-top placeholder :title="$t('BasicSetting')" left-arrow fixed @click-left="onClickLeft" />
    <van-pull-refresh v-model="isLoading" :pulling-text="$t('PullingText')" :loosing-text="$t('PullingText')" :success-text="$t('refreshSuccee')" @refresh="onRefresh">
      <van-collapse class="marginTop15" v-model="activeNames">
        <van-collapse-item :title="$t('Datetime')" name="1">
          <datetimePTVue :times="LocalArr.datetimes"></datetimePTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('EPSMuteSet')" name="2">
          <languagePTVue :columns="toggleArr" :setValue="LocalArr.EPSMuteValue" :reg="22"></languagePTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('SystemSwitch')" name="3">
          <languagePTVue :columns="toggleArr" :setValue="LocalArr.SystemSwitchValue" :reg="23"></languagePTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('ModeSelect')" name="4">
          <languagePTVue
            :setName="$t('ModeSelect')"
            :columns="solarModeArr"
            :reg="14"
            :setValue="LocalArr.SolarModeCode"
            @getFormData="setSolarMode"
          ></languagePTVue>
          <languagePT2Vue :setName="$t('EconomicModeMinSoc')" :setUntil="'%'" :columns="EconomicArr" :setValue="LocalArr.EconomicModeMinSocValue" :reg="16" :pre="1" v-if="isEconomicModel"></languagePT2Vue>
          <languagePT2Vue :setName="$t('GreenModeMinSoc')" :setUntil="'%'" :columns="EconomicArr" :setValue="LocalArr.GreenModeMinSocValue" :reg="18" :pre="1" v-if="isEcoFriendlyMode"></languagePT2Vue>
          <languagePT2Vue :setName="$t('GreenModeChrSoc')" :setUntil="'%'" :columns="EconomicArr" :setValue="LocalArr.GreenModeChrSocValue" :reg="19" :pre="1" v-if="isEcoFriendlyMode"></languagePT2Vue>
          <languagePT2Vue :setName="$t('EaseModeMinSoc')" :setUntil="'%'" :columns="EaseArr" :setValue="LocalArr.EaseModeMinSocValue" :reg="15" :pre="1" v-if="isReliefMode"></languagePT2Vue>
          <languagePTVue :setName="$t('ManualMode')" :columns="ManualArr" :setValue="LocalArr.ManualModeValue" :reg="24" v-if="isManualMode"></languagePTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('ChrgAndDischrgPeriod')" name="5">
          <div class="pltxt">{{$t('ChargeStartTime')}}</div>
          <van-field v-model="LocalArr.ChargeStartTimeValue" clickable readonly @click="showChargeStart = true">
            <template #button>
              <van-button size="small" round @click="setChargeStart($event)" class="setbtn">{{$t('Save')}}</van-button>
            </template>
          </van-field>
          <van-popup v-model="showChargeStart" round position="bottom">
            <van-datetime-picker
              v-model="LocalArr.ChargeStartTimeValue"
              type="time"
              :filter="filterTimes"
              @cancel="showChargeStart = false"
              @confirm="ChargeStartConfirm"
              :confirm-button-text="$t('OK')"
              :cancel-button-text="$t('Cancel')" 
            />
          </van-popup>
          <div class="pltxt">{{$t('ChargeEndTime')}}</div>
          <van-field v-model="LocalArr.ChargeEndTimeValue" clickable readonly @click="showChargeEnd = true">
            <template #button>
              <van-button size="small" round @click="setChargeEnd($event)" class="setbtn">{{$t('Save')}}</van-button>
            </template>
          </van-field>
          <van-popup v-model="showChargeEnd" round position="bottom">
            <van-datetime-picker
              v-model="LocalArr.ChargeEndTimeValue"
              type="time"
              :filter="filterTimes"
              @cancel="showChargeEnd = false"
              @confirm="ChargeEndConfirm"
              :confirm-button-text="$t('OK')"
              :cancel-button-text="$t('Cancel')"
            />
          </van-popup>
          <div class="pltxt">{{$t('DischargeStartTime')}}</div>
          <van-field v-model="LocalArr.DischargeStartTimeValue" clickable readonly @click="showDischargeStart = true">
            <template #button>
              <van-button size="small" round @click="setDischargeStart($event)" class="setbtn">{{$t('Save')}}</van-button>
            </template>
          </van-field>
          <van-popup v-model="showDischargeStart" round position="bottom">
            <van-datetime-picker
              v-model="LocalArr.DischargeStartTimeValue"
              type="time"
              :filter="filterTimes"
              @cancel="showDischargeStart = false"
              @confirm="DischargeStartConfirm"
              :confirm-button-text="$t('OK')"
              :cancel-button-text="$t('Cancel')"
            />
          </van-popup>
          <div class="pltxt">{{$t('DischargeEndTime')}}</div>
          <van-field v-model="LocalArr.DischargeEndTimeValue" clickable readonly @click="showDischargeEnd = true">
            <template #button>
              <van-button size="small" round @click="setDischargeEnd($event)" class="setbtn">{{$t('Save')}}</van-button>
            </template>
          </van-field>
          <van-popup v-model="showDischargeEnd" round position="bottom">
            <van-datetime-picker
              v-model="LocalArr.DischargeEndTimeValue"
              type="time"
              :filter="filterTimes"
              @cancel="showDischargeEnd = false"
              @confirm="DischargeEndConfirm"
              :confirm-button-text="$t('OK')"
              :cancel-button-text="$t('Cancel')"
            />
          </van-popup>
          <van-divider>{{$t('DischargePeriod2')}}</van-divider>
          <languagePTVue
            :setName="$t('TimePeriod2Switch')"
            :columns="toggleArr"
            :setValue="LocalArr.ChargingTimeSwitchCode"
            :reg="31"
            @getFormData="setChargingTimeSwitch"
          ></languagePTVue>
          <div v-if="isChargingTimeSwitch">
            <div class="pltxt">{{$t('DischargeStartTime')}}</div>
            <van-field v-model="LocalArr.DischargeStartTimeValue2" clickable readonly @click="showDischargeStart2 = true">
              <template #button>
                <van-button size="small" round @click="setDischargeStart2($event)" class="setbtn">{{$t('Save')}}</van-button>
              </template>
            </van-field>
            <van-popup v-model="showDischargeStart2" round position="bottom">
              <van-datetime-picker
                v-model="LocalArr.DischargeStartTimeValue2"
                type="time"
                :filter="filterTimes"
                @cancel="showDischargeStart2 = false"
                @confirm="DischargeStartConfirm2"
                :confirm-button-text="$t('OK')"
                :cancel-button-text="$t('Cancel')" 
              />
            </van-popup>
            <div class="pltxt">{{$t('DischargeEndTime')}}</div>
            <van-field v-model="LocalArr.DischargeEndTimeValue2" clickable readonly @click="showDischargeEnd2 = true">
              <template #button>
                <van-button size="small" round @click="setDischargeEnd2($event)" class="setbtn">{{$t('Save')}}</van-button>
              </template>
            </van-field>
            <van-popup v-model="showDischargeEnd2" round position="bottom">
              <van-datetime-picker
                v-model="LocalArr.DischargeEndTimeValue2"
                type="time"
                :filter="filterTimes"
                @cancel="showDischargeEnd2 = false"
                @confirm="DischargeEndConfirm2"
                :confirm-button-text="$t('OK')"
                :cancel-button-text="$t('Cancel')" 
              />
            </van-popup>
          </div>
        </van-collapse-item>
        <van-collapse-item :title="$t('ContractCurrent')" name="6">
          <languagePT2Vue :columns="EconomicArr" :setName="$t('ContractCurrent')" :setUntil="'A'" :setValue="LocalArr.ContractCurrentValue" :reg="39" :pre="1"></languagePT2Vue>
        </van-collapse-item>
      </van-collapse>
    </van-pull-refresh>
  </div>
</template>

<script>
import datetimePTVue from '../../../components/DateTime/DateTimePT'
import languagePTVue from '../../../components/Language/LanguagePT'
import pfLockInPointPTVue from '../../../components/PfLockInPoint/PfLockInPointPT'
import languagePT2Vue from '../../../components/Language/Language2PT';

export default {
  components:{
    datetimePTVue,
    languagePTVue,
    pfLockInPointPTVue,
    languagePT2Vue
  },
  data(){
    return {
      isLoading:false,
      activeNames:[],
      toggleArr:[this.$t('Disable'),this.$t('Enable')],
      isEconomicModel:false,
      isEcoFriendlyMode:false,
      isReliefMode:false,
      isManualMode:false,
      solarModeArr:[this.$t('EconomicModel'),this.$t('EcoFriendlyMode'),this.$t('ReliefMode'),this.$t('ManualMode')],
      EconomicArr:[10,20,30,40,50,60,70,80,90,100],
      EaseArr:[30,40,50,60,70,80,90,100],
      ManualArr:[this.$t('StopChrgDischrg'),this.$t('ManualCharging'),this.$t('ManualDischarge')],
      showChargeStart:false,
      showChargeEnd:false,
      showDischargeStart:false,
      showDischargeEnd:false,
      isChargingTimeSwitch:false,
      showDischargeStart2:false,
      showDischargeEnd2:false,
      LocalArr:{
        datetimes:'',
        EPSMuteValue:'',
        SystemSwitchValue:'',
        SolarModeCode:'',
        EconomicModeMinSocValue:'',
        GreenModeMinSocValue:'',
        GreenModeChrSocValue:'',
        EaseModeMinSocValue:'',
        ManualModeValue:'',
        ChargeStartTimeValue:'',
        ChargeEndTimeValue:'',
        DischargeStartTimeValue:'',
        DischargeEndTimeValue:'',
        ChargingTimeSwitchCode:'',
        DischargeStartTimeValue2:'',
        DischargeEndTimeValue2:'',
        ContractCurrentValue:''
      }
    }
  },
  methods: {
    onClickLeft(){
      this.$router.push({path:'/setting'});
    },
    ChargeStartConfirm(val){
      this.showChargeStart=false;
      this.LocalArr.ChargeStartTimeValue=val;
    },
    ChargeEndConfirm(val){
      this.showChargeEnd=false;
      this.LocalArr.ChargeEndTimeValue=val;
    },
    DischargeStartConfirm(val){
      this.showDischargeStart=false;
      this.LocalArr.DischargeStartTimeValue=val;
    },
    DischargeEndConfirm(val){
      this.showDischargeEnd=false;
      this.LocalArr.DischargeEndTimeValue=val;
    },
    DischargeStartConfirm2(val){
      this.showDischargeStart2=false;
      this.LocalArr.DischargeStartTimeValue2=val;
    },
    DischargeEndConfirm2(val){
      this.showDischargeEnd2=false;
      this.LocalArr.DischargeEndTimeValue2=val;
    },
    //格式化分钟
    filterTimes(type, options){
      if (type === 'minute') {
        return options.filter((option) => option % 15 === 0);
      }
      return options;
    },
    async getData(){
      this.comm.loadingShow();
      let datas = await this.SETAPI.setGetDataPT(localStorage.getItem('domain'),{'tokenId':localStorage.getItem('tokenId'),'sn':localStorage.getItem('wifiSN')});
      if(datas.result!=null&&datas.result!="tokenId不合法!"){
        let dataArr=datas.result;
        let year=dataArr[30];
        let month=dataArr[29]>=10?dataArr[29]:"0"+dataArr[29];
        let day=dataArr[28]>=10?dataArr[28]:"0"+dataArr[28];
        let hour=dataArr[27]>=10?dataArr[27]:"0"+dataArr[27];
        let minute=dataArr[26]>=10?dataArr[26]:"0"+dataArr[26];
        this.LocalArr.datetimes=`${year+2000}-${month}-${day} ${hour}:${minute}`;
        this.LocalArr.EPSMuteValue=dataArr[33];
        this.LocalArr.SystemSwitchValue=dataArr[24];
        this.LocalArr.SolarModeCode=dataArr[13];
        /*判断用户的选择*/
        if(this.LocalArr.SolarModeCode==0){
          this.isEconomicModel=true;
        }else{
          this.isEconomicModel=false;
        }
        if(this.LocalArr.SolarModeCode==1){
          this.isEcoFriendlyMode=true;
        }else{
          this.isEcoFriendlyMode=false;
        }
        if(this.LocalArr.SolarModeCode==2){
          this.isReliefMode=true;
        }else{
          this.isReliefMode=false;
        }
        if(this.LocalArr.SolarModeCode==3){
          this.isManualMode=true;
        }else{
          this.isManualMode=false;
        }
        this.LocalArr.EconomicModeMinSocValue=dataArr[15];
        this.LocalArr.GreenModeMinSocValue=dataArr[17];
        this.LocalArr.GreenModeChrSocValue=dataArr[18];
        this.LocalArr.EaseModeMinSocValue=dataArr[14];
        this.LocalArr.ManualModeValue=dataArr[34];

        let workMode1StartHour=this.comm.bit8low(1,dataArr[37]);
        let workMode1StartMinute=this.comm.bit8high(1,dataArr[37]);
        workMode1StartHour=parseInt(workMode1StartHour)>9?workMode1StartHour:"0"+workMode1StartHour;
        if(workMode1StartMinute>=0&&workMode1StartMinute<=14){
          workMode1StartMinute="00";
        }else if(workMode1StartMinute>=15&&workMode1StartMinute<=29){
          workMode1StartMinute="15";
        }else if(workMode1StartMinute>=30&&workMode1StartMinute<=44){
          workMode1StartMinute="30";
        }else if(workMode1StartMinute>=45&&workMode1StartMinute<=59){
          workMode1StartMinute="45";
        }
        this.LocalArr.ChargeStartTimeValue=`${workMode1StartHour}:${workMode1StartMinute}`;

        let workMode1EndHour=this.comm.bit8low(1,dataArr[38]);
        let workMode1EndMinute=this.comm.bit8high(1,dataArr[38]);
        workMode1EndHour=parseInt(workMode1EndHour)>=10?workMode1EndHour:"0"+workMode1EndHour;
        if(workMode1EndMinute>=0&&workMode1EndMinute<=14){
          workMode1EndMinute="00";
        }else if(workMode1EndMinute>=15&&workMode1EndMinute<=29){
          workMode1EndMinute="15";
        }else if(workMode1EndMinute>=30&&workMode1EndMinute<=44){
          workMode1EndMinute="30";
        }else if(workMode1EndMinute>=45&&workMode1EndMinute<=59){
          workMode1EndMinute="45";
        }
        this.LocalArr.ChargeEndTimeValue=`${workMode1EndHour}:${workMode1EndMinute}`;

        let workMode1DischargeStartHour=this.comm.bit8low(1,dataArr[39]);
        let workMode1DischargeStartMinute=this.comm.bit8high(1,dataArr[39]);
        workMode1DischargeStartHour=parseInt(workMode1DischargeStartHour)>=10?workMode1DischargeStartHour:"0"+workMode1DischargeStartHour;
        if(workMode1DischargeStartMinute>=0&&workMode1DischargeStartMinute<=14){
          workMode1DischargeStartMinute="00";
        }else if(workMode1DischargeStartMinute>=15&&workMode1DischargeStartMinute<=29){
          workMode1DischargeStartMinute="15";
        }else if(workMode1DischargeStartMinute>=30&&workMode1DischargeStartMinute<=44){
          workMode1DischargeStartMinute="30";
        }else if(workMode1DischargeStartMinute>=45&&workMode1DischargeStartMinute<=59){
          workMode1DischargeStartMinute="45";
        }
        this.LocalArr.DischargeStartTimeValue=`${workMode1DischargeStartHour}:${workMode1DischargeStartMinute}`;

        let workMode1DischargeEndHour=this.comm.bit8low(1,dataArr[40]);
        let workMode1DischargeEndMinute=this.comm.bit8high(1,dataArr[40]);
        workMode1DischargeEndHour=parseInt(workMode1DischargeEndHour)>=10?workMode1DischargeEndHour:"0"+workMode1DischargeEndHour;
        if(workMode1DischargeEndMinute>=0&&workMode1DischargeEndMinute<=14){
          workMode1DischargeEndMinute="00";
        }else if(workMode1DischargeEndMinute>=15&&workMode1DischargeEndMinute<=29){
          workMode1DischargeEndMinute="15";
        }else if(workMode1DischargeEndMinute>=30&&workMode1DischargeEndMinute<=44){
          workMode1DischargeEndMinute="30";
        }else if(workMode1DischargeEndMinute>=45&&workMode1DischargeEndMinute<=59){
          workMode1DischargeEndMinute="45";
        }
        this.LocalArr.DischargeEndTimeValue=`${workMode1DischargeEndHour}:${workMode1DischargeEndMinute}`;

        this.LocalArr.ChargingTimeSwitchCode=dataArr[41];
        if(this.LocalArr.ChargingTimeSwitchCode==1){
          this.isChargingTimeSwitch=true;
        }else{
          this.isChargingTimeSwitch=false;
        }

        let workMode2DischargeStartHour=this.comm.bit8low(1,dataArr[44]);
        let workMode2DischargeStartMinute=this.comm.bit8high(1,dataArr[44]);
        workMode2DischargeStartHour=parseInt(workMode2DischargeStartHour)>=10?workMode2DischargeStartHour:"0"+workMode2DischargeStartHour;
        if(workMode2DischargeStartMinute>=0&&workMode2DischargeStartMinute<=14){
          workMode2DischargeStartMinute="00";
        }else if(workMode2DischargeStartMinute>=15&&workMode2DischargeStartMinute<=29){
          workMode2DischargeStartMinute="15";
        }else if(workMode2DischargeStartMinute>=30&&workMode2DischargeStartMinute<=44){
          workMode2DischargeStartMinute="30";
        }else if(workMode2DischargeStartMinute>=45&&workMode2DischargeStartMinute<=59){
          workMode2DischargeStartMinute="45";
        }
        this.LocalArr.DischargeStartTimeValue2=`${workMode2DischargeStartHour}:${workMode2DischargeStartMinute}`;

        let workMode2DischargeEndHour=this.comm.bit8low(1,dataArr[45]);
        let workMode2DischargeEndMinute=this.comm.bit8high(1,dataArr[45]);
        workMode2DischargeEndHour=parseInt(workMode2DischargeEndHour)>=10?workMode2DischargeEndHour:"0"+workMode2DischargeEndHour;
        if(workMode2DischargeEndMinute>=0&&workMode2DischargeEndMinute<=14){
          workMode2DischargeEndMinute="00";
        }else if(workMode2DischargeEndMinute>=15&&workMode2DischargeEndMinute<=29){
          workMode2DischargeEndMinute="15";
        }else if(workMode2DischargeEndMinute>=30&&workMode2DischargeEndMinute<=44){
          workMode2DischargeEndMinute="30";
        }else if(workMode2DischargeEndMinute>=45&&workMode2DischargeEndMinute<=59){
          workMode2DischargeEndMinute="45";
        }
        this.LocalArr.DischargeEndTimeValue2=`${workMode2DischargeEndHour}:${workMode2DischargeEndMinute}`;

        this.LocalArr.ContractCurrentValue=dataArr[49];
      }else{
        this.comm.tipsShow('InvNoResponse');
      }
    },
    //设置SolarMode
    setSolarMode(data){
      /*判断用户的选择*/
      if(data==0){
        this.isEconomicModel=true;
      }else{
        this.isEconomicModel=false;
      }
      if(data==1){
        this.isEcoFriendlyMode=true;
      }else{
        this.isEcoFriendlyMode=false;
      }
      if(data==2){
        this.isReliefMode=true;
      }else{
        this.isReliefMode=false;
      }
      if(data==3){
        this.isManualMode=true;
      }else{
        this.isManualMode=false;
      }
    },
    //设置ChargeStart
    setChargeStart(e){
      e.stopPropagation();
      let timeArr=this.LocalArr.ChargeStartTimeValue.split(':');
      this.comm.setDataPT(1,[{'reg':27,'val':timeArr[1]*256+Number(timeArr[0])}]);
    },
    //设置ChargeEnd
    setChargeEnd(e){
      e.stopPropagation();
      let timeArr=this.LocalArr.ChargeEndTimeValue.split(':');
      this.comm.setDataPT(1,[{'reg':28,'val':timeArr[1]*256+Number(timeArr[0])}]);
    },
    //设置DischargeStart
    setDischargeStart(e){
      e.stopPropagation();
      let timeArr=this.LocalArr.DischargeStartTimeValue.split(':');
      this.comm.setDataPT(1,[{'reg':29,'val':timeArr[1]*256+Number(timeArr[0])}]);
    },
    //设置DischargeEnd
    setDischargeEnd(e){
      e.stopPropagation();
      let timeArr=this.LocalArr.DischargeEndTimeValue.split(':');
      this.comm.setDataPT(1,[{'reg':30,'val':timeArr[1]*256+Number(timeArr[0])}]);
    },
    //设置ChargingTimeSwitch
    setChargingTimeSwitch(data){
      if(data==1){
        this.isChargingTimeSwitch=true;
      }else{
        this.isChargingTimeSwitch=false;
      }
    },
    //设置时段2DischargeStart
    setDischargeStart2(e){
      e.stopPropagation();
      let timeArr=this.LocalArr.DischargeStartTimeValue2.split(':');
      this.comm.setDataPT(1,[{'reg':34,'val':timeArr[1]*256+Number(timeArr[0])}]);
    },
    //设置时段2DischargeEnd
    setDischargeEnd2(e){
      e.stopPropagation();
      let timeArr=this.LocalArr.DischargeEndTimeValue2.split(':');
      this.comm.setDataPT(1,[{'reg':35,'val':timeArr[1]*256+Number(timeArr[0])}]);
    },
    onRefresh(){
      this.isLoading = false;
      for(let i in this.LocalArr){
        this.LocalArr[i]='';
      }
      this.getData();
    }
  },
  mounted(){
    this.getData();
  }
}
</script>

<style scoped lang="less">

</style>
